package day8;

import java.util.Random;

/**
 * @author oldliu
 * @since 1.0
 */
public class Taker2 extends Thread {
    private final Account ac;

    public Taker2(String name, Account ac) {
        super(name);
        this.ac = ac;
    }

    @Override
    public void run() {
        Random r = new Random();
        while (true) {
            int m = r.nextInt(200);
            System.out.println(this.getName() + "打算取钱：" + m + "元");
            synchronized (ac) {
                while (ac.getMoney() < m) {
                    System.out.println(super.getName() + "发现钱不够，等着");
                    try {
                        ac.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(this.getName()+"被喊醒，检查账号!");
                }
                this.ac.withDraw(m);
                System.out.println(this.getName() + "打算取钱：" + m + "元成功，余额是" + ac.getMoney() + "元");
            }

        }

    }
}
